#-*- coding:utf-8 -*-
from PySide.QtGui import *
from PySide.QtCore import *
import sys
#文本使用utf-8 编码
QTextCodec.setCodecForTr(QTextCodec.codecForName("utf8"))

#定义对话框
class StandardDailog(QDialog):
    #初始化
    def __init__(self,parent=None):
        #调用父级
        super(StandardDailog, self).__init__(parent)

        self.setWindowTitle("Standard Dialog")
        #定义按钮
        filePushButton = QPushButton(self.tr("文件对话框"))
        colorPushButton = QPushButton(self.tr("颜色对话框"))
        fontPushButton = QPushButton(self.tr("字体对话框"))
        #定义文本
        self.fileLineEdit = QLineEdit()
        #定义颜色窗体
        self.colorFrame = QFrame()
        #使用box布局
        self.colorFrame.setFrameShape(QFrame.Box)
        self.colorFrame.setAutoFillBackground(True)
        self.fontLineEdit = QLineEdit("Hello World")

        #使用网格布局
        layout = QGridLayout()
        #类似x轴，y轴
        #把组件加到布局里
        layout.addWidget(filePushButton,0,0)
        layout.addWidget(self.fileLineEdit,0,1)
        layout.addWidget(colorPushButton,1,0)
        layout.addWidget(self.colorFrame,1,1)
        layout.addWidget(fontPushButton,2,0)
        layout.addWidget(self.fontLineEdit,2,1)
        #定义窗体布局
        self.setLayout(layout)

        #定义事件及响应
        self.connect(filePushButton,SIGNAL("clicked()"),self.openFile);
        self.connect(colorPushButton,SIGNAL("clicked()"),self.openColor)
        self.connect(fontPushButton,SIGNAL("clicked()"),self.openFont)

    def openFile(self):
        #定义打开文件
        s = QFileDialog.getOpenFileName(self,"Open file dialog","/","Python files(*.py)")
        self.fileLineEdit.setText(str(s))

    def openColor(self):
        #定义打开颜色处理
        c = QColorDialog.getColor(Qt.blue)
        if c.isValid():
            self.colorFrame.setPalette(QPalette(c))

    def openFont(self):
        #定义打开字体
        f,ok=QFontDialog.getFont()
        if ok:
            self.fontLineEdit.setFont(f)

app = QApplication(sys.argv)
form = StandardDailog()
form.show()
sys.exit(app.exec_())